package TCP;


import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author Cheng-02
 * @version V1.0
 * @Package test
 * @date 2024/3/20 10:06
 */
public class Day05_Client {
    public static void main(String[] args) {
        //客户端
        //
        //Socket(InetAddress address, int port)
        //创建流套接字并将其连接到指定IP地址的指定端口号。
        try {
            InetAddress address = InetAddress.getLocalHost();
            Socket socket = new Socket(address, 8888);
            System.out.println("客户端连接成功");


            //2.客户端要发送数据到服务器去，socket套接字是两台机器间通信的端点

            //最终转换的效果
            //原始字节流
            OutputStream outputStream = socket.getOutputStream();//字节流

            //数据流（字节流）
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            //文本输出流
            PrintWriter printWriter = new PrintWriter(dataOutputStream,true);
            String word = "hello word";
            //输出


            printWriter.println(word);
            System.out.println("##################");
            InputStream inputStream = socket.getInputStream();
            //转换为字符流
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            //存储在缓存区类中
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            //显示客户端内容
            String Upper = bufferedReader.readLine();
            System.out.println(Upper);




        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
